﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using CrystalDecisions.CrystalReports.Engine;

using BLL;
using DAL;
using Models;
using System.Data;
using System.Text;
using System.Xml;
using Gift.WebApp;


public partial class backstage_user : System.Web.UI.Page
{
    // Fields

    protected User mUser = new User();
    protected string ot = "";

    protected string registerIP = "";
    protected string registerTime = "";
    protected UserManager um = new UserManager();
    protected UserRankManager urm = new UserRankManager();

    // Methods
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        string pathAndQuery = HttpContext.Current.Request.Url.PathAndQuery;
        try
        {
            this.mUser.RankId = Convert.ToInt32(base.Request.Form["userRank"]);
            if (this.txtUserPwd.Value != "")
            {
                this.mUser.UserPwd = Gift.Security.DESEncrypt.Encrypt(this.txtUserPwd.Value);
            }
            this.mUser.Email = this.txtEmail.Value;
            this.mUser.PayPoints = Convert.ToInt32(this.txtPayPoints.Value);
            this.mUser.Money = Convert.ToDecimal(this.txtMoney.Value);
            this.mUser.State = Convert.ToInt32(base.Request.Form["state"]);
            if ((base.Request.QueryString["ot"] == "ed") && (base.Request.QueryString["id"] != null))
            {
                this.mUser.Id = Convert.ToInt32(base.Request.QueryString["id"]);
                if (this.um.ModifyUser(this.mUser))
                {
                    base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"更新成功！\");location.href=\"user_list.aspx\";</script>");
                }
                else
                {
                    base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>selectIsExitItem(\"selectUserRank\",\"" + base.Request.Form["userRank"] + "\");alert(\"更新失败！\");</script>");
                }
            }
            else
            {
                this.mUser.UserName = this.txtUserName.Value;
                this.mUser.RegisterIP = new ClinetMAC().GetIPAddress();
                this.mUser.LanguageVersion = this.Session["language_version"].ToString();
                if (this.um.AddUser(this.mUser))
                {
                    base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"添加成功！\");location.href=\"user_list.aspx\";</script>");
                }
                else
                {
                    base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>selectIsExitItem(\"selectUserRank\",\"" + base.Request.Form["userRank"] + "\");alert(\"添加失败！\");</script>");
                }
            }
        }
        catch
        {
            if ((base.Request.QueryString["ot"] == "ed") && (base.Request.QueryString["id"] != null))
            {
                base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>selectIsExitItem(\"selectUserRank\",\"" + base.Request.Form["userRank"] + "\");alert(\"更新失败！\");</script>");
            }
            else
            {
                base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>selectIsExitItem(\"selectUserRank\",\"" + base.Request.Form["userRank"] + "\");alert(\"添加失败！\");</script>");
            }
        }
    }

    protected StringBuilder GetTitle()
    {
        StringBuilder builder = new StringBuilder();
        try
        {
            if ((base.Request.QueryString["ot"] != null) && (base.Request.QueryString["id"] != null))
            {
                builder.AppendLine("编辑会员");
                return builder;
            }
            builder.AppendLine("添加会员");
        }
        catch
        {
        }
        return builder;
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if ((HttpContext.Current.Session["admin_user"] == null) && (HttpContext.Current.Session["language_version"] == null))
        {
            base.ClientScript.RegisterStartupScript(base.GetType(), "sJs", "top.location=\"login.html\";", true);
        }
        if (!base.IsPostBack && (base.Request.QueryString["ot"] != null))
        {
            if ((base.Request.QueryString["ot"] == "ed") && (base.Request.QueryString["id"] != null))
            {
                this.UserBind();
                base.ClientScript.RegisterStartupScript(base.GetType(), "sRti", "$(\"#trRegisterTime\").show();$(\"#trRegisterIP\").show();", true);
            }
            else
            {
                this.rdState1.Checked = true;
                base.ClientScript.RegisterStartupScript(base.GetType(), "sRti", "$(\"#trRegisterTime\").hide();$(\"#trRegisterIP\").hide();", true);
            }
        }
    }

    private void UserBind()
    {
        try
        {
            DataTable userById = this.um.GetUserById(Convert.ToInt32(base.Request.QueryString["id"]));
            if (userById.Rows.Count > 0)
            {
                this.txtUserName.Value = userById.Rows[0]["userName"].ToString();
                this.txtUserName.Attributes.Add("readonly", "readonly");
                this.txtEmail.Value = userById.Rows[0]["email"].ToString();
                this.txtPayPoints.Value = userById.Rows[0]["payPoints"].ToString();
                this.txtMoney.Value = decimal.Round(Convert.ToDecimal(userById.Rows[0]["money"]), 2).ToString();
                base.ClientScript.RegisterStartupScript(base.GetType(), "", "<script type=\"text/javascript\">selectIsExitItem(\"selectUserRank\",\"" + userById.Rows[0]["userRank"].ToString() + "\");</script>");
                if (Convert.ToInt32(userById.Rows[0]["state"]) == 1)
                {
                    this.rdState1.Checked = true;
                }
                else
                {
                    this.rdState0.Checked = true;
                }
                this.registerTime = userById.Rows[0]["registerTime"].ToString();
                this.registerIP = userById.Rows[0]["registerIP"].ToString();
            }
        }
        catch
        {
        }
    }

    protected StringBuilder UserRankBind()
    {
        StringBuilder builder = new StringBuilder();
        try
        {
            DataTable allUserRank = this.urm.GetAllUserRank();
            foreach (DataRow row in allUserRank.Rows)
            {
                if (Convert.ToInt32(row["isDefault"]) == 1)
                {
                    builder.AppendLine("<option value=\"" + row["id"].ToString() + "\" selected=\"selected\">" + row["rankName"].ToString() + "</option>");
                }
                else
                {
                    builder.AppendLine("<option value=\"" + row["id"].ToString() + "\">" + row["rankName"].ToString() + "</option>");
                }
            }
            return builder;
        }
        catch
        {
            return builder;
        }
    }

}